<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <TreeTools :node-data="company" :is-root="true" @addDepts="addDepts" />
        <hr>
        <el-tree
          :data="depts"
          :props="{label:'name'}"
          default-expand-all
        >
          <template #default="{data}">
            <TreeTools :node-data="data" @delDepts="delDepts" @addDepts="addDepts" @editDepts="editDepts" />
          </template>
        </el-tree>
      </el-card>
      <AddDepts ref="addDepts" :dialog-show.sync="dialogShow" :node-data="nodeData" @addDepts="getDeptsList" />
    </div>
  </div>
</template>

<script>
import { delDepartments, getDeptsList } from '@/api/departments'
import TreeTools from './components/tree-tools.vue'
import AddDepts from './components/add-depts.vue'
import { arr2tree } from '@/utils'
export default {
  components: {
    TreeTools, AddDepts
  },
  data() {
    return {
      company: {
        name: '江苏传智播客教育科技股份有限公司',
        manager: '负责人',
        id: ''
      },
      depts: [],
      dialogShow: false,
      nodeData: {}// 当前树形行部门数据
    }
  },
  async created() {
    this.getDeptsList()
  },
  methods: {
    // 获取部门列表
    async getDeptsList() {
      const { depts } = await getDeptsList()
      this.depts = arr2tree(depts, '')
    },
    // 删除部门
    async delDepts(id) {
      await this.$confirm('真的要删除部门嘛？')
      await delDepartments(id)
      this.getDeptsList()
    },
    // 新增部门
    addDepts(nodeData) {
      this.nodeData = nodeData
      this.dialogShow = true
    },
    // 编辑部门
    editDepts(nodeData) {
      this.nodeData = nodeData
      this.dialogShow = true
      this.$refs.addDepts.getDepartDetail(nodeData.id)
    }
  }
}
</script>
<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
